home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / edit / jwpsrc.zip / CONVERT.C < prev    next >
C/C++ Source or Header  |  1993-03-31  |  56KB  |  1,897 lines

  1. /* Copyright (C) Stephen Chung, 1991-1993.  All rights reserved. */
  2.  
  3. /* This distribution of JWP makes use of the Kana-to-Kanji conversion  */
  4. /* dictionary of the Kyoto University "Wnn" project.  The original wnn */
  5. /* copyright follows...                                                */
  6.  
  7. /*
  8.  * Copyright Kyoto University Research Institute for Mathematical Sciences
  9.  *                 1987, 1988, 1989, 1990, 1991
  10.  * Copyright OMRON Corporation. 1987, 1988, 1989, 1990, 1991
  11.  * Copyright ASTEC, Inc. 1987, 1988, 1989, 1990, 1991
  12.  *
  13.  * Permission to use, copy, modify, distribute, and sell this software
  14.  * and its documentation for any purpose is hereby granted without fee,
  15.  * provided that all of the following conditions are satisfied:
  16.  *
  17.  * 1) The above copyright notices appear in all copies
  18.  * 2) Both those copyright notices and this permission notice appear
  19.  *    in supporting documentation
  20.  * 3) The name of "Wnn" isn't changed unless substantial modifications
  21.  *    are made, or
  22.  * 3') Following words followed by the above copyright notices appear
  23.  *    in all supporting documentation of software based on "Wnn":
  24.  *
  25.  *   "This software is based on the original version of Wnn developed by
  26.  *    Kyoto University Research Institute for Mathematical Sciences (KURIMS),
  27.  *    OMRON Corporation and ASTEC Inc."
  28.  *
  29.  * 4) The names KURIMS, OMRON and ASTEC not be used in advertising or
  30.  *    publicity pertaining to distribution of the software without
  31.  *    specific, written prior permission
  32.  *
  33.  * KURIMS, OMRON and ASTEC make no representations about the suitability
  34.  * of this software for any purpose.  It is provided "as is" without
  35.  * express or implied warranty.
  36.  *
  37.  * Wnn consortium is one of distributors of the official Wnn source code
  38.  * release.  Wnn consortium also makes no representations about the
  39.  * suitability of this software for any purpose.  It is provided "as is"
  40.  * without express or implied warranty.
  41.  *
  42.  * KURIMS, OMRON, ASTEC AND WNN CONSORTIUM DISCLAIM ALL WARRANTIES WITH
  43.  * REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
  44.  * MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL KURIMS, OMRON, ASTEC OR
  45.  * WNN CONSORTIUM BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
  46.  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  47.  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  48.  * TORTUOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  49.  * PERFORMANCE OF THIS SOFTWARE.
  50.  *
  51.  */
  52.  
  53. #include "jwp.h"
  54.  
  55.  
  56. #define INDEXLEVEL      3
  57. #define SEPARATION      (SYSFONT->width / 2)
  58. #define CACHESIZE       (5L * 1024L)
  59.  
  60. typedef struct {
  61.     BYTE key[INDEXLEVEL+1];
  62.     LONG int offset;
  63. } INDEX;
  64.  
  65. typedef struct {
  66.     BYTE keylen;
  67.     BYTE choicelen;
  68.     union {
  69.         BYTE string[4];
  70.         BYTE far *ptr;
  71.     } key;
  72.     union {
  73.         KANJI string[2];
  74.         KANJI far *ptr;
  75.     } choice;
  76. } CHOICE;
  77.  
  78.  
  79.  
  80. static OFSTRUCT dctof;
  81.  
  82. static BYTE far *index = NULL;
  83. static LONG int far *offset = NULL;
  84. static int nr_index = 0;
  85. static int words[BUFSIZE];
  86.  
  87. static CHOICE far *ConvCache = NULL;
  88. static int ConvCacheSize = 0;
  89. static long int ConvRequests = 0L, ConvHits = 0L, ConvUsage = 0L, ConvChoiceChanges = 0L;
  90.  
  91. static BOOL sizing = FALSE;
  92.  
  93. KANJI kanji_list[MAXLINELEN];
  94.  
  95. typedef struct KanjiListStruct {
  96.     KANJI far *kanji;
  97.     struct KanjiListStruct far *prev, far *next;
  98. } KANJILIST;
  99.  
  100. typedef struct UserConvListStruct {
  101.     BYTE far *kana;
  102.     KANJILIST far *list;
  103.     struct UserConvListStruct far *prev, far *next;
  104. } USERCONV;
  105.  
  106. static USERCONV far *UserConversions = NULL;
  107. static USERCONV far *CurrentConv = NULL;
  108. static KANJILIST far *CurrentKanji = NULL;
  109. static BOOL UserConvChanged = FALSE;
  110. static BOOL AfterHitTest = FALSE;
  111.  
  112.  
  113.  
  114. int bytelen (BYTE far *p)
  115. {
  116.     int i;
  117.  
  118.     for (i = 0; *p; i++, p++);
  119.  
  120.     return (i);
  121. }
  122.  
  123.  
  124.  
  125. int bytecmp (BYTE far *p1, BYTE far *p2)
  126. {
  127.     for (; *p1 == *p2; p1++, p2++) {
  128.         if (*p1 == 0) return (0);
  129.     }
  130.  
  131.     return (*p1 - *p2);
  132. }
  133.  
  134.  
  135. int bytencmp (BYTE far *p1, BYTE far *p2, int n)
  136. {
  137.     int i;
  138.  
  139.     for (i = 0; i < n; i++) {
  140.         if (p1[i] != p2[i]) return (p1[i] - p2[i]);
  141.         if (p1[i] == 0) return (0);
  142.     }
  143.  
  144.     return (0);
  145. }
  146.  
  147.  
  148. KANJI far *kanjicpy (KANJI far *p1, KANJI far *p2)
  149. {
  150.     while (*p1++ = *p2++);
  151.  
  152.     return (p1);
  153. }
  154.  
  155.  
  156.  
  157. int kanjicmp (KANJI far *p1, KANJI far *p2)
  158. {
  159.     for (; *p1 == *p2; p1++, p2++) {
  160.         if (*p1 == 0) return (0);
  161.     }
  162.  
  163.     return (*p1 - *p2);
  164. }
  165.  
  166.  
  167. int kanjincmp (KANJI far *p1, KANJI far *p2, int n)
  168. {
  169.     int i;
  170.  
  171.     for (i = 0; i < n; i++) {
  172.         if (p1[i] != p2[i]) return (p1[i] - p2[i]);
  173.         if (p1[i] == 0) return (0);
  174.     }
  175.  
  176.     return (0);
  177. }
  178.  
  179.  
  180. int kanjilen (KANJI far *s)
  181. {
  182.     int i;
  183.  
  184.     for (i = 0; *s; s++, i++);
  185.     return (i);
  186. }
  187.  
  188.  
  189. int unitlen (UNIT far *s)
  190. {
  191.     int i;
  192.  
  193.     for (i = 0; s->kanji; s++, i++);
  194.     return (i);
  195. }
  196.  
  197.  
  198. KANJI far *kanjicat (KANJI far *d, KANJI far *s)
  199. {
  200.     kanjicpy(d + kanjilen(d), s);
  201.     return (d);
  202. }
  203.  
  204.  
  205. static int ConvHash (BYTE far *key)
  206. {
  207.     int i;
  208.     long int sum;
  209.  
  210.     for (i = 0, sum = 0L; key[i]; i++) sum += key[i];
  211.  
  212.     return (sum % ConvCacheSize);
  213. }
  214.  
  215.  
  216.  
  217. static int indexcmp (BYTE far *p1, BYTE far *p2)
  218. {
  219.     int i;
  220.  
  221.     for (i = 0; i < INDEXLEVEL; i++) {
  222.         if (p1[i] != p2[i]) return (p1[i] - p2[i]);
  223.         if (p1[i] == '\0') return (0);
  224.     }
  225.  
  226.     return (0);
  227. }
  228.  
  229.  
  230.  
  231. static void ReadUserConversions (char *fname)
  232. {
  233.     int fd;
  234.     int i, j, nr_conv, nr_list, n;
  235.     OFSTRUCT of;
  236.     USERCONV far *up;
  237.     KANJILIST far *kp;
  238.  
  239.     fd = OpenFile(fname, &of, OF_READ);
  240.     if (fd < 0) return;
  241.  
  242.     lseek(fd, 0L, 0);
  243.  
  244.     read(fd, &nr_conv, sizeof(int));
  245.  
  246.     for (i = 0; i < nr_conv; i++) {
  247.         if (UserConversions == NULL) {
  248.             UserConversions = up = StructAlloc(USERCONV);
  249.             up->prev = up->next = NULL;
  250.         } else {
  251.             up->next = StructAlloc(USERCONV);
  252.             up->next->prev = up;
  253.             up = up->next;
  254.             up->next = NULL;
  255.         }
  256.         up->list = NULL;
  257.  
  258.         read (fd, &n, sizeof(int));
  259.         up->kana = BlockAlloc(n);
  260.         _lread(fd, up->kana, n);
  261.  
  262.         read (fd, &nr_list, sizeof(int));
  263.  
  264.         for (j = 0; j < nr_list; j++) {
  265.             if (up->list == NULL) {
  266.                 up->list = kp = StructAlloc(KANJILIST);
  267.                 kp->prev = kp->next = NULL;
  268.             } else {
  269.                 kp->next = StructAlloc(KANJILIST);
  270.                 kp->next->prev = kp;
  271.                 kp = kp->next;
  272.                 kp->next = NULL;
  273.             }
  274.  
  275.             read (fd, &n, sizeof(int));
  276.             kp->kanji = BlockAlloc(n * sizeof(KANJI));
  277.             _lread(fd, kp->kanji, n * sizeof(KANJI));
  278.         }
  279.     }
  280.  
  281.     close(fd);
  282. }
  283.  
  284.  
  285.  
  286. void WriteUserConversions (char *fname)
  287. {
  288.     int fd, n;
  289.     OFSTRUCT of;
  290.     USERCONV far *up;
  291.     KANJILIST far *kp;
  292.  
  293.     if (!UserConvChanged) return;
  294.  
  295.     fd = OpenFile(fname, &of, OF_CREATE | OF_WRITE);
  296.     if (fd < 0) {
  297.         ErrorMessage(global.hwnd, "WARNING: Cannot write User Conversion Dictionary '%s'!", fname);
  298.         return;
  299.     }
  300.  
  301.     lseek(fd, 0L, 0);
  302.  
  303.     /* How many conversions? */
  304.  
  305.     for (up = UserConversions, n = 0; up != NULL; up = up->next, n++);
  306.     write(fd, &n, sizeof(int));
  307.  
  308.     for (up = UserConversions; up != NULL; up = up->next) {
  309.         n = bytelen(up->kana) + 1;
  310.         write(fd, &n, sizeof(int));
  311.         _lwrite(fd, up->kana, n);
  312.  
  313.         /* How many kanji's? */
  314.  
  315.         for (kp = up->list, n = 0; kp != NULL; kp = kp->next, n++);
  316.         write(fd, &n, sizeof(int));
  317.  
  318.         for (kp = up->list; kp != NULL; kp = kp->next) {
  319.             n = kanjilen(kp->kanji) + 1;
  320.             write(fd, &n, sizeof(int));
  321.             _lwrite(fd, kp->kanji, n * sizeof(KANJI));
  322.         }
  323.     }
  324.  
  325.     close(fd);
  326. }
  327.  
  328.  
  329.  
  330. int InitConversion (void)
  331. {
  332.     int fd;
  333.     int bytes, n, blocks;
  334.     unsigned int i, j;
  335.     BYTE far *cp;
  336.     INDEX far *ip;
  337.     OFSTRUCT idxof;
  338.  
  339.  
  340.     kanji_list[0] = 0;
  341.  
  342.     fd = OpenFile (global.userdict, &idxo